package com.example.yls.demoa;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button value;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button DIVvalue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        value=findViewById(R.id.btn_1);
        btn1=findViewById(R.id.btn1);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn4=findViewById(R.id.btn4);
        btn5=findViewById(R.id.btn5);
        DIVvalue=findViewById(R.id.btn_2);
        value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Point start=new Point(100,100);
                Point end=new Point(400,600);
                ValueAnimator valueAnimator=ValueAnimator.ofObject(new PointEvaluator(),
                        start,end);
                valueAnimator.setDuration(3000);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        Point point= (Point) valueAnimator.getAnimatedValue();
                        Log.e(TAG,"point x="+point.getX()+"point y="+point.getY());
                        value.setTranslationX(point.getX());
                        value.setTranslationY(point.getY());
                        value.requestLayout();

                    }
                });
                valueAnimator.start();
            }
        });
        //平移
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*float currentX=value.getTranslationX();
                ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(value,
                       "translationX",currentX,500 );
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                       float value1= (float) valueAnimator.getAnimatedValue();
                       value.setTranslationY(value1);
                    }
                });
                objectAnimator.start();*/
                //自定义按钮属性
                MyColor start=new MyColor();
                start.red=255;
                MyColor end=new MyColor();
                end.green=255;
                ObjectAnimator objectAnimator=ObjectAnimator.ofObject(
                        DIVvalue,"myColor",new MyColorEvaluator()
                        ,start,end);
                objectAnimator.setDuration(5000);
                objectAnimator.start();

            }
        });
        //缩放
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(value,
                        "scaleX",1,5 );
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value1= (float) valueAnimator.getAnimatedValue();
                        value.setTranslationY(value1);
                    }
                });
                objectAnimator.start();

            }
        });
        //旋转
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(value,
                        "rotation",0,360 );
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value1= (float) valueAnimator.getAnimatedValue();
                        value.setTranslationY(value1);
                    }
                });
                objectAnimator.start();
            }
        });
        //透明度
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               /* ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(value,
                        "Alpha",1,0,1 );
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float value1= (float) valueAnimator.getAnimatedValue();
                        value.setTranslationY(value1);
                    }
                });
                objectAnimator.start();*/
               //xml定义
                Animator animator= AnimatorInflater.loadAnimator(
                        MainActivity.this,R.animator.alpha);
                animator.setTarget(value);
                animator.start();
            }
        });
        //动画组合
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AnimatorSet animatorSet=new AnimatorSet();
                float currentX=value.getTranslationX();
                ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(value,
                        "translationX",currentX,500 );
                ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(value,
                        "scaleX",1,5 );
                ObjectAnimator objectAnimator3=ObjectAnimator.ofFloat(value,
                        "rotation",0,360 );
                ObjectAnimator objectAnimator4=ObjectAnimator.ofFloat(value,
                        "Alpha",1,0,1 );
                animatorSet.play(objectAnimator1).with(objectAnimator2).before(objectAnimator3).after(objectAnimator4);
                animatorSet.setDuration(5000);
                animatorSet.start();
            }
        });
        //自定义按钮属性

    }
}
